home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 326-350 / disk_337 / cmanual / sprites.lzh / Sprites / Example1.c < prev    next >
C/C++ Source or Header  |  1990-01-30  |  9KB  |  286 lines

  1. /* Example1                                                            */
  2. /* This program shows how to declare and initialize some sprite data   */
  3. /* and a SimpleSprite structure. It also shows how to reserve a sprite */
  4. /* (sprite 2), and how to move it around. The user moves the sprite by */
  5. /* pressing the arrow keys.                                            */
  6.  
  7.  
  8.  
  9. #include <intuition/intuition.h>
  10. /* Include this file since you are using sprites: */
  11. #include <graphics/sprite.h>
  12.  
  13.  
  14.  
  15. /* Declare the functions we are going to use: */
  16. void main();
  17. void free_memory();
  18.  
  19.  
  20.  
  21. struct IntuitionBase *IntuitionBase = NULL;
  22. /* We need to open the Graphics library since we are using sprites: */
  23. struct GfxBase *GfxBase = NULL;
  24.  
  25.  
  26.  
  27. /* Declare a pointer to a Window structure: */ 
  28. struct Window *my_window = NULL;
  29.  
  30. /* Declare and initialize your NewWindow structure: */
  31. struct NewWindow my_new_window=
  32. {
  33.   40,            /* LeftEdge    x position of the window. */
  34.   20,            /* TopEdge     y positio of the window. */
  35.   200,           /* Width       200 pixels wide. */
  36.   100,           /* Height      100 lines high. */
  37.   0,             /* DetailPen   Text should be drawn with colour reg. 0 */
  38.   1,             /* BlockPen    Blocks should be drawn with colour reg. 1 */
  39.   CLOSEWINDOW|   /* IDCMPFlags  The window will give us a message if the */
  40.   RAWKEY,        /*             user has selected the Close window gad, */
  41.                  /*             or if the user has pressed a key. */
  42.   SMART_REFRESH| /* Flags       Intuition should refresh the window. */
  43.   WINDOWCLOSE|   /*             Close Gadget. */
  44.   WINDOWDRAG|    /*             Drag gadget. */
  45.   WINDOWDEPTH|   /*             Depth arrange Gadgets. */
  46.   WINDOWSIZING|  /*             Sizing Gadget. */
  47.   ACTIVATE,      /*             The window should be Active when opened. */
  48.   NULL,          /* FirstGadget No Custom gadgets. */
  49.   NULL,          /* CheckMark   Use Intuition's default CheckMark. */
  50.   "SPRITES",     /* Title       Title of the window. */
  51.   NULL,          /* Screen      Connected to the Workbench Screen. */
  52.   NULL,          /* BitMap      No Custom BitMap. */
  53.   80,            /* MinWidth    We will not allow the window to become */
  54.   30,            /* MinHeight   smaller than 80 x 30, and not bigger */
  55.   300,           /* MaxWidth    than 300 x 200. */
  56.   200,           /* MaxHeight */
  57.   WBENCHSCREEN   /* Type        Connected to the Workbench Screen. */
  58. };
  59.  
  60.  
  61.  
  62. /*********************************************************************/
  63. /* Extra information:                                                */
  64. /* When we declare the window pointer, the intuition library pointer */
  65. /* etc, we initialize them to point to NULL:                         */
  66. /* struct Window *my_window = NULL;                                  */
  67. /* Since we then know that all of the pointers will point to NULL    */
  68. /* when we start, we can check if they still point to NULL when we   */
  69. /* quit. If they do not point to NULL anymore, we close that window, */
  70. /* library etc.                                                      */
  71. /*********************************************************************/
  72.  
  73.  
  74.  
  75. /********************************************************/
  76. /* 1. Declare and initialize some sprite graphics data: */
  77. /********************************************************/
  78. UWORD chip my_sprite_data[36]=
  79. {
  80.   0x0000, 0x0000,
  81.  
  82.   0x0180, 0x0000,
  83.   0x03C0, 0x0000,
  84.   0x07E0, 0x0000,
  85.   0x0FF0, 0x0000,
  86.   0x1FF8, 0x0000,
  87.   0x3FFC, 0x0000,
  88.   0x7FFE, 0x0000,
  89.   0x0000, 0xFFFF,
  90.   0x0000, 0xFFFF,
  91.   0x7FFE, 0x7FFE,
  92.   0x3FFC, 0x3FFC,
  93.   0x1FF8, 0x1FF8,
  94.   0x0FF0, 0x0FF0,
  95.   0x07E0, 0x07E0,
  96.   0x03C0, 0x03C0,
  97.   0x0180, 0x0180,
  98.  
  99.   0x0000, 0x0000
  100. };
  101.  
  102.  
  103.  
  104. /*******************************************************/
  105. /* 2. Declare and initialize a SimpleSprite structure: */
  106. /*******************************************************/
  107. struct SimpleSprite my_sprite=
  108. {
  109.   my_sprite_data, /* posctldata, pointer to the sprite data. */
  110.   16,             /* height, 16 lines tall. */
  111.   40, 60,         /* x, y, position on the screen. */
  112.   -1,             /* num, this field is automatically initialized when  */
  113.                   /* you call the GetSprite() function, so we set it to */
  114.                   /* -1 for the moment.                                 */
  115. };
  116.  
  117.  
  118.  
  119. void main()
  120. {
  121.   /* Sprite position: */
  122.   WORD x = my_sprite.x;
  123.   WORD y = my_sprite.y;
  124.  
  125.   /* Direction of the sprite: */
  126.   WORD x_direction = 0;
  127.   WORD y_direction = 0;
  128.  
  129.   /* Boolean variable used for the while loop: */
  130.   BOOL close_me = FALSE;
  131.  
  132.   ULONG class; /* IDCMP */
  133.   USHORT code; /* Code */
  134.  
  135.   /* Declare a pointer to an IntuiMessage structure: */
  136.   struct IntuiMessage *my_message;
  137.  
  138.  
  139.  
  140.   /* Open the Intuition Library: */
  141.   IntuitionBase = (struct IntuitionBase *)
  142.     OpenLibrary( "intuition.library", 0 );
  143.   
  144.   if( IntuitionBase == NULL )
  145.     free_memory(); /* Could NOT open the Intuition Library! */
  146.  
  147.  
  148.  
  149.   /* Since we are using sprites we need to open the Graphics Library: */
  150.   /* Open the Graphics Library: */
  151.   GfxBase = (struct GfxBase *)
  152.     OpenLibrary( "graphics.library", 0);
  153.  
  154.   if( GfxBase == NULL )
  155.     free_memory(); /* Could NOT open the Graphics Library! */
  156.  
  157.  
  158.  
  159.   /* We will now try to open the window: */
  160.   my_window = (struct Window *) OpenWindow( &my_new_window );
  161.   
  162.   /* Have we opened the window succesfully? */
  163.   if(my_window == NULL)
  164.     free_memory(); /* Could NOT open the Window! */
  165.  
  166.  
  167.  
  168.   /* Change the colour register 21 - 23: */
  169.   SetRGB4( &my_window->WScreen->ViewPort, 21, 0xF, 0x0, 0x0 ); /* Red */
  170.   SetRGB4( &my_window->WScreen->ViewPort, 22, 0xF, 0xF, 0x0 ); /* Yellow */
  171.   SetRGB4( &my_window->WScreen->ViewPort, 23, 0x0, 0xF, 0x0 ); /* Green */
  172.  
  173.  
  174.  
  175.   /*******************************/
  176.   /* 3. Try to reserve sprite 2: */
  177.   /*******************************/
  178.   if( GetSprite( &my_sprite, 2 ) != 2 )
  179.     free_memory(); /* Could not reserve sprite number 2. */
  180.  
  181.  
  182.  
  183.   /* Stay in the while loop until the user has selected the Close window */
  184.   /* gadget: */
  185.   while( close_me == FALSE )
  186.   {
  187.     /* Stay in the while loop as long as we can collect messages */
  188.     /* sucessfully: */
  189.     while(my_message = (struct IntuiMessage *) GetMsg(my_window->UserPort))
  190.     {
  191.       /* After we have collected the message we can read it, and save any */
  192.       /* important values which we maybe want to check later: */
  193.       class = my_message->Class;
  194.       code  = my_message->Code;
  195.  
  196.  
  197.       /* After we have read it we reply as fast as possible: */
  198.       /* REMEMBER! Do never try to read a message after you have replied! */
  199.       /* Some other process has maybe changed it. */
  200.       ReplyMsg( my_message );
  201.  
  202.  
  203.       /* Check which IDCMP flag was sent: */
  204.       switch( class )
  205.       {
  206.         case CLOSEWINDOW:     /* Quit! */
  207.                close_me=TRUE;
  208.                break;  
  209.  
  210.         case RAWKEY:          /* A key was pressed! */
  211.                /* Check which key was pressed: */
  212.                switch( code )
  213.                {
  214.                  /* Up Arrow: */
  215.                  case 0x4C:      y_direction = -1; break; /* Pressed */
  216.                  case 0x4C+0x80: y_direction = 0;  break; /* Released */
  217.  
  218.                  /* Down Arrow: */
  219.                  case 0x4D:      y_direction = 1; break; /* Pressed */
  220.                  case 0x4D+0x80: y_direction = 0; break; /* Released */
  221.  
  222.                  /* Right Arrow: */
  223.                  case 0x4E:      x_direction = 1; break; /* Pressed */
  224.                  case 0x4E+0x80: x_direction = 0; break; /* Released */
  225.  
  226.                  /* Left Arrow: */
  227.                  case 0x4F:      x_direction = -1; break; /* Pressed */
  228.                  case 0x4F+0x80: x_direction = 0;  break; /* Released */
  229.                }
  230.                break;  
  231.       }
  232.     }
  233.  
  234.  
  235.  
  236.     /* Change the x/y position: */
  237.     x += x_direction;
  238.     y += y_direction;
  239.  
  240.     /* Check that the sprite does not move outside the screen: */
  241.     if(x > 320)
  242.       x = 320;
  243.     if(x < 0)
  244.       x = 0;
  245.     if(y > 200)
  246.       y = 200;
  247.     if(y < 0)
  248.       y = 0;
  249.  
  250.     /* Move the sprite: */
  251.     MoveSprite( 0, &my_sprite, x, y );
  252.  
  253.  
  254.  
  255.     /* Wait for the videobeam to reach the top of the display: (This */
  256.     /* will slow down the animation so the user can see the sprite)  */
  257.     WaitTOF();
  258.   }
  259.  
  260.  
  261.  
  262.   /* Free all allocated memory: (Close the window, libraries etc) */
  263.   free_memory();
  264.  
  265.   /* THE END */
  266. }
  267.  
  268.  
  269.  
  270. /* This function frees all allocated memory. */
  271. void free_memory()
  272. {
  273.   if( my_sprite.num != -1 )
  274.     FreeSprite( my_sprite.num );
  275.  
  276.   if( my_window )
  277.     CloseWindow( my_window );
  278.   
  279.   if( GfxBase )
  280.     CloseLibrary( GfxBase );
  281.  
  282.   if( IntuitionBase )
  283.     CloseLibrary( IntuitionBase );
  284.  
  285.   exit();
  286. }